package com.us.usws.site.ws.resource;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.video.Video;
import com.us.usws.model.domain.video.VideoComment;
import com.us.usws.service.ro.VideoCommentROService;
import com.us.usws.service.ro.VideoROService;
import com.us.usws.site.ws.entity.WSEntity;
import com.us.usws.site.ws.entity.video.WSVideoEntityPackGenerator;
import com.us.usws.support.uitls.ServiceFactory;
import com.us.usws.support.uitls.SimpleDataPageUtil;

/**
 * User webservice
 */
@Path("/movies")
public class VideoResource {

	//private static Log log = LogFactory.getFactory().getInstance(VideoResource.class);

	private VideoROService vService = ServiceFactory.getInstance().getService(VideoROService.class);
	private VideoCommentROService vcService = ServiceFactory.getInstance().getService(VideoCommentROService.class);

	@GET
	@Path("/{videoUid}.json")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public WSEntity getVideoInfo(@PathParam("videoUid") String videoUid) {
		Video video = vService.getVideo(videoUid);

		WSEntity ret = WSVideoEntityPackGenerator.getInstance().getVideoInfoVideoPack(video);

		return ret;
	}

	@GET
	@Path("/{videoUid}/comments.json")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public WSEntity getVideoComments(@PathParam("videoUid") String videoUid, @QueryParam("page_size") int pageSize,
			@QueryParam("page") int pageNo) {

		if (pageSize < 1) {
			pageSize = SiteConstants.DFT_WS_PAGE_SIZE;
		}
		if (pageNo < 1) {
			pageNo = 1;
		}

		int cnt = vcService.getCommentsCountByVideo(videoUid);
		SimpleDataPageUtil pu = new SimpleDataPageUtil(pageSize, cnt);
		pu.setPageNo(pageNo);
		List<VideoComment> results = vcService.getCommentsByVideo(videoUid, pu.getStart(), pageSize);

		WSEntity ret = WSVideoEntityPackGenerator.getInstance().getVideoCommentsPack(videoUid, results, pu);

		return ret;
	}

}
